/**
 * Created by 61741 on 2018/2/9.
 */
var scope='';
var userType=2;
var city = "default";
var serachClik = true;//判断城市选择是顶部还是搜索过滤
$(function () {
    alert(page);
     if(page=="agency"){
        scope="AGENCY";
        userType=3;
        loadflagShop("AGENCY", city, "starCompany");
        loadflagShop("AGENCY_ADVICE", city, "adviceShop");
        loadflagShop("AGENCY_HOTSERACH", city, "hotSearchshop");
    }else if(page=="agency"){
        scope="PARTS";
        userType=4;
        loadflagShop("PARTS", city, "starCompany");
        loadflagShop("PARTS_ADVICE", city, "adviceShop");
        loadflagShop("PARTS_HOTSERACH", city, "hotSearchshop");
    }
    else{
        scope="SELLER";
        userType=2;
        loadflagShop("SELLER", city, "starCompany");
        loadflagShop("SELLER_ADVICE", city, "adviceShop");
        loadflagShop("SELLER_HOTSERACH", city, "hotSearchshop");

    }

//        $("#city").chosen({width: 380})
    loadSlider(city);

    loadallshop();
})
function loadSlider(city) {
    var data = {city: city};
    asyncAjax({
        url: ctx + "/home/slider/list/" + scope + "?" + $.param(data).replace(/(%5D|%5B)/g, ""),
        success: function (res) {
            if (res["result"] == "0") {
                if (!isEmpty(res["data"])) {
                    $("#slider-ul").html("");
                    $("#slider-tml").tmpl(res["data"]).appendTo($("#slider-ul"));
                    // 修正轮播图插件图片宽度自适应问题

                    $(".slider-img").attr("style", "width:" + $(".slider-img-ul li").length * 100 + "% !important;");
                    $(".slider-img-ul>li>img").css({
                        width: $(".slider-wrap").width() + "px !important",
                        height: "auto !important"
                    });
                    $(".slider").xSlider({w: 1340});
                }
                else {
                    $("#newGood-table").html('<li><img src="${ctx}/resources/portal/img/banner/1-banner.jpg"></li>');
                }
            }
            else {
                bootbox.alert(res["msg"]);
            }
        }

    })
}
function loadflagShop(type, city, target) {
    var data = {pageType: type, city: city,};
    asyncAjax({
        url: ctx + "/portal/shop/flag/list?" + $.param(data).replace(/(%5D|%5B)/g, ""),
        beforeSend: function (req) {
            $("#" + target).html('<i class="fa fa-spin fa-spinner"></i>正在刷新数据...');
        },
        success: function (res) {
            console.log("res:%o", res);
            if (res["result"] == "0") {
                $("#" + target).html("");
                if (!isEmpty(res["data"])) {
                    $("#" + target + "-tml").tmpl(res["data"]).appendTo($("#" + target));

                } else {
                    $("#" + target).html("<div class='alert alert-warning'>该模块尚未配置推广商铺</div>");
                }
            } else {
                bootbox.alert(res["msg"]);


            }
        }
    })
}
function loadallshop(pageNum) {
    var data = {reviewStatus: 2, pageNum: pageNum || 1, pageSize: 10};
    asyncAjax({
        url: ctx + "/user/mgr/list/"+userType+"?" + $.param(data).replace(/(%5D|%5B)/g, ""),
        beforeSend: function (req) {
            $("#companyList").html('<i class="fa fa-spin fa-spinner"></i>正在刷新数据...');
        },
        success: function (res) {
            if (res["result"] == "0") {
                $("#companyList").html("");
                if (!isEmpty(res["data"])) {
                    $("#companyList-tml").tmpl(res["data"]).appendTo($("#companyList"));

                    $("#page").paging({
                        pageNo: pageNum || 1,
                        totalPage: Math.ceil(res.total / 10),
                        totalSize: res.total,
                        callback: function (num) {
                            loadallshop(num);
                        }
                    })

                } else {
                    $("#companyList").html("<div class='alert alert-warning'>该模块尚未配置推广商铺</div>");
                }
            } else {
                bootbox.alert(res["msg"]);
            }
        }
    })

}
function searchSub(name, el, pageNum) {
    var data = {userType:userType};
    if (name == "keyword") {
        data = {keyword: $("#" + el).val(), userType: userType, pageNum: pageNum || 1, pageSize: 10};
    }
    if (name == "cityId") {
        data = {cityId: $("." + el).attr('value'), userType: userType, pageNum: pageNum || 1, pageSize: 10};
    }
    if (name == "saleCount") {
        data = {sort: name, userType: userType, pageNum: pageNum || 1, pageSize: 10};
        $(".fa-sort-amount-desc").css({
            color: "#F34B91"
        })
    }
    asyncAjax({
        url: ctx + "/user/mgr/getUserList?" + $.param(data).replace(/(%5D|%5B)/g, ""),
        success: function (res) {
            if (res["result"] == "0") {
                $("#companyList").html("");
                if (!isEmpty(res["data"])) {
                    $("#companyList-tml").tmpl(res["data"]).appendTo($("#companyList"));

                    $("#page").paging({
                        pageNo: pageNum || 1,
                        totalPage: Math.ceil(res.total / 10),
                        totalSize: res.total,
                        callback: function (num) {
                            searchSub(name, el, num);
                        }
                    })

                } else {
                    $("#companyList").html("<div class='alert alert-warning'>未搜到相关商家</div>");
                }
            } else {
                bootbox.alert(res["msg"]);
            }
        }
    })

}
function getCity(provinceId) {
    $(".areaSelect a.active,.areaSelect .city-select.active").removeClass("active");
    $(".areaSelect a[code=\"" + provinceId + "\"]").addClass("active");
    var targetDiv = $(".areaSelect a[code=\"" + provinceId + "\"]").nextAll(".city-select")[0];
    $(targetDiv).addClass("active");

    var data = {provinceId: provinceId}
    asyncAjax({
        url: ctx + "/area/getAllcity?" + $.param(data).replace(/(%5D|%5B)/g, ""),
        beforeSend: function () {
            $(targetDiv).html("");
        },
        success: function (res) {
            if (res["result"] == "0") {
                if (!isEmpty(res["data"])) {
                    var html = "";
                    $.each(res["data"], function (idx, item) {
                        html += "<a href='javascript:setCity("+item["addressId"]+","+"\""+ item["shortName"]+"\")' value=\"" + item["addressId"] + "\">" + item["shortName"] + "</a>";
                    })
                    $(targetDiv).html(html);
                } else {
                }
            } else {
                bootbox.alert(res["msg"]);
            }
        }
    })
}
function setCity(cityId,shortName) {
    $(".city-select a[value=\"" + cityId + "\"]").removeClass("active");
    $(".city-select a[value=\"" + cityId + "\"]").addClass("active");
    if(serachClik){
    $('#fillterCity').attr('value',cityId);
    $('#fillterCity').html(shortName);
    searchSub('cityId','city-lable',1);
    }
    else{

    }
    $("#areaSlectBox").modal("hide");
}
function showAreaDialog(el) {
    var id = $(el).attr('id');
    if(id !="fillterCity"){
        serachClik=false;
    }
    $("#areaSlectBox").modal("show");
}
function changecheck(obj) {
    var aa = document.getElementsByName("seller");
    for (var i = 0; i < aa.length; i++) {
        aa[i].checked = false;
    }
    obj.checked = true;
}
//分页

/*收藏*/
function collect(collectid) {
    var celflag = document.getElementById(collectid).alt;


    if (celflag == 0) {

        document.getElementById(collectid).src = "${ctx}/resources/portal/img/after_collect.png";
        document.getElementById(collectid).alt = "1";
    }
    else {
        document.getElementById(collectid).src = "${ctx}/resources/portal/img/before_collect.png";
        document.getElementById(collectid).alt = "0";
    }

}